package com.wyth.lottery.utils;

import android.content.Context;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;

import java.io.IOException;

public class UsbPrinter {

	private static final String TAG = "UsbPrinter";

	private final UsbDevice mDevice;
	private final UsbDeviceConnection mConnection;
	private final UsbInterface mInterface;
	private final UsbEndpoint mEndpoint;



	private static final int TRANSFER_TIMEOUT = 200;

	public UsbPrinter(Context context, UsbDevice device) throws IOException {
		UsbInterface iface = null;
		UsbEndpoint epout = null;

		for (int i = 0; i < device.getInterfaceCount(); i++) {
			iface = device.getInterface(i);
			if (iface == null)
				throw new IOException("failed to get interface " + i);

			int epcount = iface.getEndpointCount();
			for (int j = 0; j < epcount; j++) {
				UsbEndpoint ep = iface.getEndpoint(j);
				if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
					epout = ep;
					break;
				}
			}

			if (epout != null)
				break;
		}

		if (epout == null) {
			throw new IOException("no output endpoint.");
		}

		mDevice = device;
		mInterface = iface;
		mEndpoint = epout;

		UsbManager usbman = (UsbManager) context
				.getSystemService(Context.USB_SERVICE);
		mConnection = usbman.openDevice(device);

		if (mConnection == null) {
			throw new IOException("failed to open usb device.");
		}

		mConnection.claimInterface(mInterface, true);
	}

	public void write(byte[] data) throws IOException {


		if (mConnection.bulkTransfer(mEndpoint, data, data.length,
				TRANSFER_TIMEOUT) >16384)


			throw new IOException("failed to write usb endpoint.");
	}

	public void close() {
		mConnection.releaseInterface(mInterface);
		mConnection.close();
	}


	public static enum ALIGNMENT {
		LEFT, CENTER, RIGHT,
	}

	public static enum FONT {
		FONT_A,
		FONT_B,
	}

	public void selectAlignment(ALIGNMENT alignment) throws IOException {
		int iAlignment = 0;

		switch (alignment) {
			case LEFT:
				iAlignment = 0;
				break;
			case CENTER:
				iAlignment = 1;
				break;
			case RIGHT:
				iAlignment = 2;
				break;
			default:
				iAlignment = 0;
		}

		String command = String.format("ESC a %d", iAlignment);
		byte[] b = CommandUtil.convertEscposToBinary(command);
		if (b != null)
			write(b);
	}

	public void printString(String string, FONT font, Boolean bold, Boolean underlined, Boolean doubleHeight, Boolean doubleWidth) throws IOException {

		int options = 0;
		String command = "";
		byte[] b = new byte[]{};

		b = CommandUtil.convertEscposToBinary(command);
		if (b != null)
			write(b);

	    command = String.format("'%s' LF", "" );
	      b = CommandUtil.convertEscposToBinary(command);
	      if(b != null)
		      write(b);
	}


	public void cutPaper(Context context) throws IOException {
		/*Resources resources = context.getResources();
		android.graphics.Bitmap bmp1 = BitmapFactory.decodeResource(resources, R.drawable.test02);*/

		android.graphics.Bitmap bmp1 = BitmapT.bitmaptest1(context);


		byte[] data = new byte[]{0x1B, 0x33, 0x00};
		try {
			write(data);
		} catch (IOException e) {
			e.printStackTrace();
		}
		data[0] = (byte) 0x00;
		data[1] = (byte) 0x00;
		data[2] = (byte) 0x00;    //重置参数

		int pixelColor;

		// ESC * m nL nH 点阵图
		byte[] escBmp = new byte[]{0x1B, 0x2A, 0x00, 0x00, 0x00};
		try {
			write(escBmp);
		} catch (IOException e) {
			e.printStackTrace();
		}
		escBmp[2] = (byte) 0x21;
		//nL, nH
		escBmp[3] = (byte) (bmp1.getWidth() % 256);
		escBmp[4] = (byte) (bmp1.getWidth() / 256);

		// 每行进行打印
		for (int i = 0; i < bmp1.getHeight() / 24 + 1; i++) {
			try {
				write(escBmp);
			} catch (IOException e) {
				e.printStackTrace();
			}

			for (int j = 0; j < bmp1.getWidth(); j++) {
				for (int k = 0; k < 24; k++) {
					if (((i * 24) + k) < bmp1.getHeight()) {
						pixelColor = bmp1.getPixel(j, (i * 24) + k);
						if (pixelColor <-8388608) {
							data[k / 8] += (byte) (128 >> (k % 8));
						}
					}
				}
				write(data);
				data[0] = (byte)0x00;
				data[1] = (byte)0x00;
				data[2] = (byte)0x00;
				// Clear to Zero.
			}
           //换行，打印第二行
			byte[] data2 = { 0x00 };
			write(data2);
		}

	}
	public void cutPaper() throws IOException
	{
		String command = "GS V 65 20";
		byte[] b = CommandUtil.convertEscposToBinary(command);
		if(b != null)
			write(b);
	}
}
	/*public void cutPaper() throws IOException
	{
		String command = "GS V 65 20";
		byte[] b = CommandUtil.convertEscposToBinary(command);
		if(b != null)
			write(b);
	}*/

